package com.wtpm.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fwk.spbt.annotation.NoTokenCheckAnno;
import com.fwk.spbt.constant.SpbtRspEnum;
import com.fwk.spbt.controller.LoginController;
import com.fwk.spbt.exception.BsException;
import com.fwk.spbt.vo.RequestVO;
import com.fwk.spbt.vo.ResponeVO;
import com.wtpm.sys.entity.SysUser;
import com.wtpm.sys.service.IUserService;
import com.wtpm.sys.vo.LoginMenuVO;
import com.wtpm.sys.vo.LoginUserVO;
import com.wtpm.sys.vo.WTPMUserVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/sys/login")
@Api(tags = { "sys->登陆" })
public class SysLoginController extends LoginController {

	@Autowired
	private IUserService userService;

	@CrossOrigin
	@ApiOperation(value = "用户登录")
	@RequestMapping(value = "/in", method = { RequestMethod.POST })
	@NoTokenCheckAnno
	public ResponeVO<String> login(@RequestBody LoginUserVO reqDTO) {
		if (!checkVerifycode(reqDTO.getVsKey(), reqDTO.getVsCode()))
			throw new BsException(SpbtRspEnum.SPBT_00004, reqDTO.getVsKey());

		SysUser sysUser = userService.checkUser(reqDTO.getUserId(), reqDTO.getPassword());
		if (sysUser == null) {
			throw new BsException(SpbtRspEnum.SPBT_00002, reqDTO.getVsKey());
		}

		WTPMUserVO user = new WTPMUserVO();
		user.setUserId(sysUser.getId());
		user.setUserName(sysUser.getName());
		List<LoginMenuVO> menus = userService.queryMenuByUser(sysUser.getId());
		user.setRoutes(menus);
		String token = redisCacheUser(user);
		return response(token);
	}

	@CrossOrigin
	@ApiOperation(value = "用户登出")
	@RequestMapping(value = "/out", method = { RequestMethod.POST })
	public ResponeVO<String> logout(@RequestBody RequestVO reqVO) {
		redisDelteUser(reqVO.getToken());
		return response();
	}

	@ApiOperation(value = "用户信息")
	@RequestMapping(value = "/info", method = { RequestMethod.POST })
	@NoTokenCheckAnno
	public ResponeVO<WTPMUserVO> info(@RequestBody RequestVO reqVO) {
		String token = reqVO.getToken();
		WTPMUserVO respDTO = WTPMUserVO.class.cast(readUserInfo(token));
		if (respDTO == null)
			throw new BsException(SpbtRspEnum.SPBT_00003, token);
		return response(respDTO);
	}

	@ApiOperation(value = "验证码")
	@RequestMapping(value = "/verifycode", method = { RequestMethod.POST })
	@NoTokenCheckAnno
	public void verifycode(HttpServletRequest request, HttpServletResponse response) {
		verifycode(response);
	}

	@ApiOperation(value = "管理系统菜单")
	@RequestMapping(value = "/menu", method = { RequestMethod.POST })
	public ResponeVO<List<LoginMenuVO>> menu(@RequestBody RequestVO reqVO) {
		WTPMUserVO userInfo = redisService.get(reqVO.getToken(), WTPMUserVO.class);
		if (userInfo.getRoutes() == null) {
			userInfo.setRoutes(userService.queryMenuByUser(reqVO.getUserId()));
		}
		return response(userInfo.getRoutes());
	}
}
